﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;


namespace LotteCinema.Admin
{
    public partial class QuanLyCaChieu : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(IsPostBack==false)
                LaydsCC();
        }
   
        public void LaydsCC()
        {
             DataTable ds_cc = Timkiem_MA("");
           
            for (int i = 0; i < ds_cc.Rows.Count; i++)
            {
                string str = ds_cc.Rows[i]["MaCC"].ToString();
                DDLMacc.Items.Add(str);
            }
            grCaChieu.DataSource = ds_cc;
            grCaChieu.DataBind();
        }

        protected void DDLMacc_SelectedIndexChanged(object sender, EventArgs e)
        {
          // string s = "select * from CACHIEU where macc='" + DDLMacc.SelectedItem.ToString() + "'";
            DataTable dt = Timkiem_MA( DDLMacc.SelectedItem.ToString());
            if (dt.Rows.Count != 0)
            {
                txtTGBD.Text = dt.Rows[0][1].ToString();
                txtTGKT.Text = dt.Rows[0][2].ToString();
            }

        }

        protected void imThem_Click(object sender, ImageClickEventArgs e)
        {
                try
                {
                    List<SqlParameter> sqlPara = new List<SqlParameter>();
                    sqlPara.Add(new SqlParameter("@thoigianbd", txtTGBD.Text.ToString()));
                    sqlPara.Add(new SqlParameter("@thoigiankt", txtTGKT.Text.ToString()));
                    CDatastore.ThucHienStore("SP_THEM_CACHIEU", sqlPara);
                    lblThongBao.Text = "Bạn đã thêm Ca Chiếu thành công";
                    DDLMacc.Items.Clear();
                    txtTGKT.Text = null;
                    txtTGBD.Text = null;
                    LaydsCC();
                }
                catch (Exception ex)
                {
                    lblThongBao.Text = ex.Message;
                }
            
        }

        protected void grCaChieu_SelectedIndexChanged(object sender, EventArgs e)
        {
            DDLMacc.Text = grCaChieu.SelectedRow.Cells[1].Text;
            txtTGBD.Text = grCaChieu.SelectedRow.Cells[2].Text;
            txtTGKT.Text = grCaChieu.SelectedRow.Cells[3].Text;
        }

        protected void imXoa_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                List<SqlParameter> sqlPara = new List<SqlParameter>();
                sqlPara.Add(new SqlParameter("@macc", DDLMacc.Text.ToString()));
                CDatastore.ThucHienStore("SP_XOA_CACHIEU", sqlPara);
                lblThongBao.Text = "Bạn đã xóa Mã Ca Chiếu " + DDLMacc.Text + " thành công";
                DDLMacc.Items.Clear();
                LaydsCC();
                txtTGBD.Text = null;
                txtTGKT.Text = null;
                
            }
            catch (Exception ex)
            {
                lblThongBao.Text = ex.Message;
            }
        }

        protected void ImageButton3_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                List<SqlParameter> sqlPara = new List<SqlParameter>();
                sqlPara.Add(new SqlParameter("@macc", DDLMacc.Text.ToString()));
                sqlPara.Add(new SqlParameter("@thoigianbd", txtTGBD.Text.ToString()));
                sqlPara.Add(new SqlParameter("@thoigiankt", txtTGKT.Text.ToString()));
                CDatastore.ThucHienStore("SP_CAPNHAT_CACHIEU", sqlPara);
                lblThongBao.Text = "Bạn đã cập nhật Mã Ca Chiếu " + DDLMacc.Text + " thành công";
                DDLMacc.Items.Clear();
                LaydsCC();
                txtTGBD.Text = null;
                txtTGKT.Text = null;
            }
            catch (Exception ex)
            {
                lblThongBao.Text = ex.Message;
            }
        }
        public DataTable Timkiem_MA(string ten)
        {
            try
            {
                if (ten != null || ten != "")
                {
                    List<SqlParameter> sqlPara = new List<SqlParameter>();
                    sqlPara.Add(new SqlParameter("@macc", ten));
                    return CDatastore.DocStore("SP_TIMKIEM_CACHIEU_MACC", sqlPara);
                }
            }
            catch (Exception e)
            {
                lblThongBao.Text = e.Message;
            }
            return CDatastore.DocStore("SP_TIMKIEM_CACHIEU_MACC");
        }
        protected void grCaChieu_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grCaChieu.PageIndex = e.NewPageIndex;
            grCaChieu.DataBind();
            LaydsCC();
        }

    }
}